/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.gr.staffpm.security.service;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gr.staffpm.datatypes.Group;
import com.gr.staffpm.datatypes.Role;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.security.dao.UserDAO;

/**
 * Default implementation of the {@link UserService} interface.  This service implements
 * operations related to User data.
 */
@Transactional
@Service("userService")
public class DefaultUserService implements UserService {

    private UserDAO userDAO;

    @Autowired
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Override
    public User getCurrentUser() {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        if (username != null) {
            // they are either authenticated or remembered from a previous session,
            // so return the user:
            return findUser(username);
        } else {
            //not logged in or remembered:
            return null;
        }
    }

    @Override
    public void createUser(String username, String email, String password) {
        User user = new User();
        user.setUsername(username);
        user.setEmailAddress(email);

        String hashedPasswordBase64 = new Sha256Hash(password, null, 1024).toBase64();

        user.setPassword(hashedPasswordBase64);
        userDAO.createUser(user);
    }

    @Override
    public List<User> getAllUsers() {
        return userDAO.getAllUsers();
    }

    @Override
    public User getUser(int userId) {
        return userDAO.getUser(userId);
    }

    @Override
    public void deleteUser(int userId) {
        userDAO.deleteUser(userId);
    }

    @Override
    public void addOrUpdateUser(User user) {
        userDAO.addOrUpdateUser(user);
    }

    @Override
    public User findUser(String username) {
        return userDAO.findUser(username);
    }

    @Override
    public List<User> getAllUsers(int first, int count, String property, boolean ascending) {
        return userDAO.getAllUsers(property, ascending).subList(first, first + count);
    }

    @Override
    public List<User> getUsersInGroupByGroupName(String groupName) {
        return userDAO.getUsersInGroupByGroupName(groupName);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.security.service.UserService#getUserRole()
     */
    @Override
    public Role getUserRole() {
        return userDAO.getUserRole();
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.security.service.UserService#addRole(com.gr.staffpm.datatypes.User, java.lang.String)
     */
    @Override
    public void addRole(User user, String roleName) {
        userDAO.addRole(user, roleName);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.security.service.UserService#getAllRoles()
     */
    @Override
    public List<Role> getAllRoles() {
        return userDAO.getAllRoles();
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.security.service.UserService#addRoles(com.gr.staffpm.datatypes.User, java.util.List)
     */
    @Override
    public void addRoles(User user, List<Role> roles) {
        userDAO.addRoles(user, roles);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.security.service.UserService#getAllGroups()
     */
    @Override
    public List<Group> getAllGroups() {
        return userDAO.getAllGroups();
    }

}
